package com.acp.service.impl;


import com.acp.context.Config;
import com.acp.dao.acp.DcsSelectAllTempMapper;
import com.acp.dao.acpo.*;
import com.acp.model.*;
import com.acp.result.JsonData;
import com.acp.result.ResultInfo;
import com.acp.result.ResultUtil;
import com.acp.service.DcsJsonService;
import com.acp.util.UUIDBuild;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.construction
 * @Description: json数据录入实现类
 * @author: 张星亮
 * @date: 2019/4/5
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class DcsJsonServiceImpl implements DcsJsonService {

    @Resource
    private DictMapper dictMapper;
    @Resource
    private RetrievalMapper retrievalMapper;

    @Resource
    private DcsInputCodeMapper dcsInputCodeMapper;

    @Resource
    private DcsPerformanceMapper dcsPerformanceMapper;

    @Resource
    private DcsCollectCodeMapper dcsCollectCodeMapper;

    @Resource
    private DcsTaskPerformanceMapper dcsTaskPerformanceMapper;

    @Resource
    private DcsSelectAllTempMapper dcsSelectAllTempMapper;

    /**
     * 解析json工具方法
     *
     * @param object
     * @param type
     * @return 返回String 类型字符串
     */
    private String doValue(String object, String type) {
        String value;
        try {
            Map mapObject = (Map) JSON.parse(object);
            value = mapObject.get(type).toString();
        } catch (Exception ex) {
            value = "";
        }

        return value;
    }

    /**
     * @D采集导入解析json
     *
     * @param object
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public ResultInfo jsonInput(String object, String importStatus, String statusNum) {
        String tableName = doValue(object, "name");
        String features = doValue(object, "features");
        String inPutStatus = doValue(object, "inPutStatus");
        switch (tableName) {
            case "吴文化地名":
                tableName = "wu_culture_place";
                break;
            case "传统产业":
                tableName = "traditional_industry";
                break;
            case "历史文化街区":
                tableName = "historical_street";
                break;
            case "历史地段":
                tableName = "historical_area";
                break;
            case "河道水系":
                tableName = "watercourse";
                break;
            case "文控保单位":
                tableName = "protection_unit";
                break;
            case "文控保单位(单体)":
                tableName = "protection_unit_individual";
                break;
            case "尚未核定为文物保护单位的不可移动文物":
                tableName = "ucr";
                break;
            case "尚未核定为文物保护单位的不可移动文物(单体)":
                tableName = "ucr_individual";
                break;
            case "历史建筑":
                tableName = "historical_building";
                break;
            case "历史建筑(单体)":
                tableName = "historical_building_individual";
                break;
            case "苏州园林":
                tableName = "suzhou_gardens";
                break;
            case "苏州园林(单体)":
                tableName = "suzhou_gardens_individual";
                break;
            case "近现代重要史迹及代表性建筑":
                tableName = "hsataoc";
                break;
            case "近现代重要史迹及代表性建筑(单体)":
                tableName = "hsataoc_individual";
                break;
            case "古城墙":
                tableName = "old_wall";
                break;
            case "传统民居":
                tableName = "traditional_houses";
                break;
            case "传统民居(单体)":
                tableName = "traditional_houses_individual";
                break;
            case "古树名木":
                tableName = "old_tree";
                break;
            case "工业遗产":
                tableName = "industrial_heritage";
                break;
            case "工业遗产(单体)":
                tableName = "industrial_heritage_individual";
                break;
            case "古构筑物":
                tableName = "old_structure";
                break;
            case "非物质文化遗产":
                tableName = "nch";
                break;
            case "整体格局与风貌":
                tableName = "layout_style";
                break;
            case "地下文物埋藏区":
                tableName = "uar";
                break;
            case "X":
                tableName = "others";
                break;
            default:
                break;
        }
        List<String> keys = dictMapper.selectColumnKeyResult(tableName);
        List<String> values = dictMapper.selectColumnValueResult(tableName);
        JSONArray array = JSONArray.parseArray(features);
        if ("historical_street".equals(tableName) || "protection_unit".equals(tableName)) {
        } else {
            if (array != null) {
                List<String> list = new ArrayList<>();
                List<String> list1 = new ArrayList<>();
                for (int a = 0; a < array.size(); a++) {
                    Map mapObject = (Map) JSON.parse(array.get(a).toString());
                    String properties = mapObject.get("properties").toString();
                    Map mapCode = (Map) JSON.parse(properties);
                    String stringCode = "";
                    if (mapCode.get("序号") != null) {
                        stringCode = mapCode.get("序号").toString();
                        list.add(stringCode);
                    }
                }
                HashMap<String, Integer> hs = new HashMap<String, Integer>();
                for (String string : list) {
                    Integer count = 1;
                    if (hs.get(string) != null) {
                        count = hs.get(string) + 1;
                    }
                    hs.put(string, count);
                }
                for (String key : hs.keySet()) {
                    if (hs.get(key) != null & hs.get(key) > 1) {
                        System.out.print(key + " ");
                        list1.add(key);
                    }
                }
                if (list1.size() > 0) {
                    return ResultUtil.createFail2(Config.MESSAGE, 913, list1);
                }
            }
        }


        if ("0".equals(inPutStatus)) {
            if (array != null) {
                List<String> list = new ArrayList<>();
                List<String> list1 = new ArrayList<>();
                for (int a = 0; a < array.size(); a++) {
                    Map mapObject = (Map) JSON.parse(array.get(a).toString());
                    String properties = mapObject.get("properties").toString();
                    Map mapCode = (Map) JSON.parse(properties);
                    String stringCode = "";
                    if (mapCode.get("序号") != null) {
                        stringCode = mapCode.get("序号").toString();
                        list.add(stringCode);
                    }
                }
                for (String tableCode : list) {
                    Retrieval retrieval = retrievalMapper.selectByName(new Retrieval(tableCode, tableName));
                    if (retrieval != null) {
                        list1.add(tableCode);
                    }
                }
                if (list1.size() > 0) {
                    return ResultUtil.createFail2(Config.MESSAGE, 914, list1);
                }
            }
        }

        if (array != null) {
            for (int i = 0; i < array.size(); i++) {
                String key1 = "";
                String values1 = "";
                String point = "";
                String Name = "";
                String code = "";
                String polygon1 = "";
                String polygon2 = "";
                String polygon3 = "";

                String singCode = "code_" + tableName;
                Map mapObject = (Map) JSON.parse(array.get(i).toString());

                String geometry = "";
                String jsonType = "";
                if (mapObject.get("geometry") != null) {
                    geometry = mapObject.get("geometry").toString();
                    Map map = (Map) JSON.parse(geometry);
                    jsonType = map.get("type").toString();
                }
                String properties = mapObject.get("properties").toString();
                Map map1 = (Map) JSON.parse(properties);
                if (map1.get("X") != null && map1.get("Y") != null) {
                    String X = map1.get("X").toString();
                    String Y = map1.get("Y").toString();
                    point = X + " " + Y;
                }

                Set<String> set = map1.keySet();
                for (String key : set) {
                    if (values.contains(key)) {
                        String val = String.valueOf(map1.get(key));
                        String n = keys.get(values.indexOf(key));
                        if ("name".equals(n)) {
                            Name = String.valueOf(map1.get(key));
                        }
                        if (keys.get(values.indexOf(key)).equals(singCode)) {
                            code = String.valueOf(map1.get(key));
                        }
                        if ("".equals(key1)) {
                            key1 += keys.get(values.indexOf(key));
                        } else {
                            key1 += "," + keys.get(values.indexOf(key));
                        }
                        if ("".equals(values1)) {
                            values1 += "'" + val + "'";
                        } else {
                            values1 += ",'" + val + "'";
                        }
                    }
                }
                if ("uar".equals(tableName)) {
                    Name = code;
                }
                Retrieval retrieval = retrievalMapper.selectByName(new Retrieval(code, tableName));

                if ("old_tree".equals(tableName) || "wu_culture_place".equals(tableName) || "uar".equals(tableName) || "traditional_industry".equals(tableName) || "old_structure".equals(tableName) || "nch".equals(tableName)) {
                    inputPoint(Name, tableName, retrieval, map1, key1, values1, point, code, importStatus, statusNum);
                } else if ("watercourse".equals(tableName) || "old_wall".equals(tableName) || "ucr".equals(tableName) || "historical_building".equals(tableName) || "suzhou_gardens".equals(tableName) || "hsataoc".equals(tableName) || "historical_area".equals(tableName) || "traditional_houses".equals(tableName) || "industrial_heritage".equals(tableName)) {
                    inputPointAndPolygon(Name, tableName, retrieval, jsonType, map1, key1, values1, point, code, geometry, importStatus, statusNum);
                } else if ("historical_street".equals(tableName) || "protection_unit".equals(tableName)) {
                    String polygonType = map1.get("范围线类型").toString();
                    inputPointAndTwoPolygon(polygonType, Name, tableName, retrieval, jsonType, map1, key1, values1, point, code, geometry, importStatus, statusNum);
                } else if ("protection_unit_individual".equals(tableName) || "ucr_individual".equals(tableName) || "historical_building_individual".equals(tableName) || "suzhou_gardens_individual".equals(tableName) || "traditional_houses_individual".equals(tableName) || "industrial_heritage_individual".equals(tableName) || "hsataoc_individual".equals(tableName)) {
                    inputPointAndPolygon(Name, tableName, retrieval, jsonType, map1, key1, values1, point, code, geometry, importStatus, statusNum);
                }
            }

            List<DcsPerformance> list = dcsPerformanceMapper.selectCode();
            if (list.size() > 0) {
                for (DcsPerformance dcsPerformance : list) {
                    String stayCount = String.valueOf(dcsPerformance.getCount());
                    String taskCode = dcsPerformance.getTask_code();
                    DcsTaskPerformance dcsTaskPerformance = DcsTaskPerformance.builder().stay_count(stayCount).task(taskCode).build();
                    List<DcsCollectCode> listNumber = dcsCollectCodeMapper.selectCountByCode(taskCode);
                    int a = listNumber.size();
                    String starsNumber = "0";
                    if (a > 0) {
                        double b = (double) (dcsPerformance.getCount() * 5) / a;
                        starsNumber = String.format("%.1f", b);
                    }
                    dcsTaskPerformance.setStars_number(starsNumber);
                    dcsTaskPerformanceMapper.updateStayCount(dcsTaskPerformance);
                }
            }
            List<DcsTaskPerformance> listAll = dcsTaskPerformanceMapper.selectAll(null);
            if (listAll.size() > 0) {
                for (DcsTaskPerformance dcsTaskPerformance : listAll) {
                    int total = Integer.valueOf(dcsTaskPerformance.getTotal_count());
                    int stay = Integer.valueOf(dcsTaskPerformance.getStay_count());
                    int complete = 0;
                    if (total >= stay) {
                        complete = total - stay;
                    }
                    String complete_count = String.valueOf(complete);
                    dcsTaskPerformance.setComplete_count(complete_count);
                    dcsTaskPerformanceMapper.updateCompleteCount(dcsTaskPerformance);
                }
            }
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
    }

    /**
     * 点录入方法
     *
     * @param Name
     * @param tableName
     * @param retrieval
     * @param map1
     * @param key1
     * @param values1
     * @param point
     * @param code
     * @return
     */
    public ResultInfo inputPoint(String Name, String tableName, Retrieval retrieval, Map map1, String key1, String values1, String point, String code, String importStatus, String statusNum) {
        if (retrieval == null) {
            Date date = new Date();
            long longtime = date.getTime();
            java.sql.Date time = new java.sql.Date(longtime);
            String id = UUIDBuild.getUUID();
            String sql1 = "insert into object_manager(id,code,name,table_name,time ,status) values ('" + id + "','" + code + "','" + Name + "','" + tableName + "','" + time + "','" + importStatus + "')";
            String sql = "";
            if (map1.get("X") != null && map1.get("Y") != null) {
                sql = "insert into " + tableName + "(id," + key1 + ",point) values ('" + id + "'," + values1 + ",ST_GeomFromText('point(" + point + ")'))";
            } else {
                sql = "insert into " + tableName + "(id," + key1 + ") values ('" + id + "'," + values1 + ")";
            }
            dictMapper.insertObjectBatch(sql);
            retrievalMapper.insertObjectBatch(sql1);
            DcsInputCode dcsInputCode = DcsInputCode.builder().id(id).code(code).status(statusNum).input_time(date).build();
            dcsInputCodeMapper.insert(dcsInputCode);

            if ("已入库".equals(importStatus)) {
                String sqlInput = "select count(1) from " + tableName + " where code_" + tableName + " = '" + code + "'";
                int i = dcsSelectAllTempMapper.select(sqlInput);
                Retrieval retrievalInput = new Retrieval();
                if (i == 0) {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("新增");
                } else {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("更新");
                }
                retrievalMapper.updateByPrimaryKeySelective(retrievalInput);
            }

        } else {
            Date date = new Date();
            long longtime = date.getTime();
            java.sql.Date time = new java.sql.Date(longtime);
            String id = retrieval.getId();
            String sql1 = "insert into object_manager(id,code,name,table_name,time ,status) values ('" + id + "','" + code + "','" + Name + "','" + tableName + "','" + time + "','" + importStatus + "')";
            String sql = "";
            if (map1.get("X") != null && map1.get("Y") != null) {
                sql = "insert into " + tableName + "(id," + key1 + ",point) values ('" + id + "'," + values1 + ",ST_GeomFromText('point(" + point + ")'))";
            } else {
                sql = "insert into " + tableName + "(id," + key1 + ") values ('" + id + "'," + values1 + ")";
            }
            String deletesql1 = "delete from object_manager where code = '" + code + "'";

            String deletesql = "delete from " + tableName + " where code_" + tableName + " = '" + code + "'";
            if ("uar".equals(tableName)) {
                deletesql = "delete from " + tableName + " where code_uar = '" + Name + "'";
            }
            dictMapper.deleteObjectBatch(deletesql);
            dictMapper.insertObjectBatch(sql);
            retrievalMapper.deleteObjectBatch(deletesql1);
            retrievalMapper.insertObjectBatch(sql1);
            DcsInputCode dcsInputCode = DcsInputCode.builder().id(id).code(code).status(statusNum).input_time(time).build();
            dcsInputCodeMapper.updateStatus(dcsInputCode);

            if ("已入库".equals(importStatus)) {
                String sqlInput = "select count(1) from " + tableName + " where code_" + tableName + " = '" + code + "'";
                int i = dcsSelectAllTempMapper.select(sqlInput);
                Retrieval retrievalInput = new Retrieval();
                if (i == 0) {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("新增");
                } else {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("更新");
                }
                retrievalMapper.updateByPrimaryKeySelective(retrievalInput);
            }
        }


        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }

    /**
     * 点和一个面录入方法
     *
     * @param Name
     * @param tableName
     * @param retrieval
     * @param jsonType
     * @param map1
     * @param key1
     * @param values1
     * @param point
     * @param code
     * @param geometry
     * @return
     */
    public ResultInfo inputPointAndPolygon(String Name, String tableName, Retrieval retrieval, String jsonType, Map map1, String key1, String values1, String point, String code, String geometry, String importStatus, String statusNum) {
        if (retrieval == null) {
            Date date = new Date();
            long longtime = date.getTime();
            java.sql.Date time = new java.sql.Date(longtime);
            String id = UUIDBuild.getUUID();
            String sql1 = "insert into object_manager(id,code,name,table_name,time ,status) values ('" + id + "','" + code + "','" + Name + "','" + tableName + "','" + time + "','" + importStatus + "')";
            String sql = "";
            if ("Polygon".equals(jsonType) || "MultiPolygon".equals(jsonType) && map1.get("X") != null && map1.get("Y") != null) {
                if (tableName.equals("traditional_houses") || tableName.equals("watercourse") || "protection_unit_individual".equals(tableName) || "ucr_individual".equals(tableName) || "historical_building_individual".equals(tableName) || "suzhou_gardens_individual".equals(tableName) || "traditional_houses_individual".equals(tableName) || "industrial_heritage_individual".equals(tableName) || "hsataoc_individual".equals(tableName)) {
                    sql = "insert into " + tableName + "(id," + key1 + ",geometry,point) values ('" + id + "'," + values1 + ",st_geomfromgeojson('" + geometry + "'),ST_GeomFromText('point(" + point + ")'))";
                } else {
                    sql = "insert into " + tableName + "(id, code," + key1 + ",geometry,point) values ('" + id + "','" + code +"'," + values1 + ",st_geomfromgeojson('" + geometry + "'),ST_GeomFromText('point(" + point + ")'))";
                }

            } else if (map1.get("X") != null && map1.get("Y") != null) {
                if (tableName.equals("traditional_houses") || tableName.equals("watercourse") || "protection_unit_individual".equals(tableName) || "ucr_individual".equals(tableName) || "historical_building_individual".equals(tableName) || "suzhou_gardens_individual".equals(tableName) || "traditional_houses_individual".equals(tableName) || "industrial_heritage_individual".equals(tableName) || "hsataoc_individual".equals(tableName)) {
                    sql = "insert into " + tableName + "(id," + key1 + ",point) values ('" + id + "'," + values1 + ",ST_GeomFromText('point(" + point + ")'))";
                } else {
                    sql = "insert into " + tableName + "(id, code," + key1 + ",point) values ('" + id + "','" + code +"'," + values1 + ",ST_GeomFromText('point(" + point + ")'))";
                }

            } else {
                if (tableName.equals("traditional_houses") || tableName.equals("watercourse") || "protection_unit_individual".equals(tableName) || "ucr_individual".equals(tableName) || "historical_building_individual".equals(tableName) || "suzhou_gardens_individual".equals(tableName) || "traditional_houses_individual".equals(tableName) || "industrial_heritage_individual".equals(tableName) || "hsataoc_individual".equals(tableName)) {
                    sql = "insert into " + tableName + "(id," + key1 + ") values ('" + id + "'," + values1 + ")";
                } else {
                    sql = "insert into " + tableName + "(id, code," + key1 + ") values ('" + id + "','" + code +"'," + values1 + ")";
                }

            }
            dictMapper.insertObjectBatch(sql);
            retrievalMapper.insertObjectBatch(sql1);
            DcsInputCode dcsInputCode = DcsInputCode.builder().id(id).code(code).status(statusNum).input_time(date).build();
            dcsInputCodeMapper.insert(dcsInputCode);

            if ("已入库".equals(importStatus)) {
                String sqlInput = "select count(1) from " + tableName + " where code_" + tableName + " = '" + code + "'";
                int i = dcsSelectAllTempMapper.select(sqlInput);
                Retrieval retrievalInput = new Retrieval();
                if (i == 0) {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("新增");
                } else {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("更新");
                }
                retrievalMapper.updateByPrimaryKeySelective(retrievalInput);
            }

        } else {
            Date date = new Date();
            long longtime = date.getTime();
            java.sql.Date time = new java.sql.Date(longtime);
            String id = retrieval.getId();
            String sql1 = "insert into object_manager(id,code,name,table_name,time ,status) values ('" + id + "','" + code + "','" + Name + "','" + tableName + "','" + time + "','" + importStatus + "')";
            String sql = "";
            if ("Polygon".equals(jsonType) || "MultiPolygon".equals(jsonType) && map1.get("X") != null && map1.get("Y") != null) {
                sql = "insert into " + tableName + "(id," + key1 + ",geometry,point) values ('" + id + "'," + values1 + ",st_geomfromgeojson('" + geometry + "'),ST_GeomFromText('point(" + point + ")'))";
            } else if (map1.get("X") != null && map1.get("Y") != null) {
                sql = "insert into " + tableName + "(id," + key1 + ",point) values ('" + id + "'," + values1 + ",ST_GeomFromText('point(" + point + ")'))";
            } else {
                sql = "insert into " + tableName + "(id," + key1 + ") values ('" + id + "'," + values1 + ")";
            }
            String deletesql1 = "delete from object_manager where code = '" + code + "'";

            String deletesql = "delete from " + tableName + " where code_" + tableName + " = '" + code + "'";
            if ("uar".equals(tableName)) {
                deletesql = "delete from " + tableName + " where code_uar = '" + Name + "'";
            }
            dictMapper.deleteObjectBatch(deletesql);
            dictMapper.insertObjectBatch(sql);
            retrievalMapper.deleteObjectBatch(deletesql1);
            retrievalMapper.insertObjectBatch(sql1);
            DcsInputCode dcsInputCode = DcsInputCode.builder().id(id).code(code).status(statusNum).input_time(time).build();
            dcsInputCodeMapper.updateStatus(dcsInputCode);

            if ("已入库".equals(importStatus)) {
                String sqlInput = "select count(1) from " + tableName + " where code_" + tableName + " = '" + code + "'";
                int i = dcsSelectAllTempMapper.select(sqlInput);
                Retrieval retrievalInput = new Retrieval();
                if (i == 0) {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("新增");
                } else {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("更新");
                }
                retrievalMapper.updateByPrimaryKeySelective(retrievalInput);
            }

        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }

    /**
     * 点和两个面录入方法
     *
     * @param Name
     * @param tableName
     * @param retrieval
     * @param jsonType
     * @param map1
     * @param key1
     * @param values1
     * @param point
     * @param code
     * @param geometry
     * @param polygonType
     * @return
     */
    public ResultInfo inputPointAndTwoPolygon(String polygonType, String Name, String tableName, Retrieval retrieval, String jsonType, Map map1, String key1, String values1, String point, String code, String geometry, String importStatus, String statusNum) {
        if (retrieval == null) {
            Date date = new Date();
            long longtime = date.getTime();
            java.sql.Date time = new java.sql.Date(longtime);
            String id = UUIDBuild.getUUID();
            String sql1 = "insert into object_manager(id,code,name,table_name,time ,status) values ('" + id + "','" + code + "','" + Name + "','" + tableName + "','" + time + "','" + importStatus + "')";
            String sql = "";
            if ("Polygon".equals(jsonType) && map1.get("X") != null && map1.get("Y") != null) {
                sql = "insert into " + tableName + "(id, code," + key1 + ",geometry,point) values ('" + id + "','" + code + "'," + values1 + ",st_geomfromgeojson('" + geometry + "'),ST_GeomFromText('point(" + point + ")'))";
            } else if (map1.get("X") != null && map1.get("Y") != null) {
                sql = "insert into " + tableName + "(id, code," + key1 + ",point) values ('" + id + "','" + code + "'," + values1 + ",ST_GeomFromText('point(" + point + ")'))";
            } else {
                sql = "insert into " + tableName + "(id, code," + key1 + ") values ('" + id + "','" + code + "'," + values1 + ")";
            }
            dictMapper.insertObjectBatch(sql);
            retrievalMapper.insertObjectBatch(sql1);
            DcsInputCode dcsInputCode = DcsInputCode.builder().id(id).code(code).status(statusNum).input_time(date).build();
            dcsInputCodeMapper.insert(dcsInputCode);

            if ("已入库".equals(importStatus)) {
                String sqlInput = "select count(1) from " + tableName + " where code_" + tableName + " = '" + code + "'";
                int i = dcsSelectAllTempMapper.select(sqlInput);
                Retrieval retrievalInput = new Retrieval();
                if (i == 0) {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("新增");
                } else {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("更新");
                }
                retrievalMapper.updateByPrimaryKeySelective(retrievalInput);
            }

        } else if ("核心保护范围".equals(polygonType)) {
            Date date = new Date();
            long longtime = date.getTime();
            java.sql.Date time = new java.sql.Date(longtime);
            String id = retrieval.getId();
            String sql1 = "insert into object_manager(id,code,name,table_name,time ,status) values ('" + id + "','" + code + "','" + Name + "','" + tableName + "','" + time + "','" + importStatus + "')";
            String sql = "";
            if ("Polygon".equals(jsonType) && map1.get("X") != null && map1.get("Y") != null) {
                sql = "insert into " + tableName + "(id," + key1 + ",geometry,point) values ('" + id + "'," + values1 + ",st_geomfromgeojson('" + geometry + "'),ST_GeomFromText('point(" + point + ")'))";
            } else if (map1.get("X") != null && map1.get("Y") != null) {
                sql = "insert into " + tableName + "(id," + key1 + ",point) values ('" + id + "'," + values1 + ",ST_GeomFromText('point(" + point + ")'))";
            } else {
                sql = "insert into " + tableName + "(id," + key1 + ") values ('" + id + "'," + values1 + ")";
            }
            String deletesql1 = "delete from object_manager where code = '" + code + "'";

            String deletesql = "delete from " + tableName + " where code_" + tableName + " = '" + code + "'";
            if ("uar".equals(tableName)) {
                deletesql = "delete from " + tableName + " where code_uar = '" + Name + "'";
            }
            dictMapper.deleteObjectBatch(deletesql);
            dictMapper.insertObjectBatch(sql);
            retrievalMapper.deleteObjectBatch(deletesql1);
            retrievalMapper.insertObjectBatch(sql1);
            DcsInputCode dcsInputCode = DcsInputCode.builder().id(id).code(code).status(statusNum).input_time(time).build();
            dcsInputCodeMapper.updateStatus(dcsInputCode);

            if ("已入库".equals(importStatus)) {
                String sqlInput = "select count(1) from " + tableName + " where code_" + tableName + " = '" + code + "'";
                int i = dcsSelectAllTempMapper.select(sqlInput);
                Retrieval retrievalInput = new Retrieval();
                if (i == 0) {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("新增");
                } else {
                    retrievalInput.setId(id);
                    retrievalInput.setOperation("更新");
                }
                retrievalMapper.updateByPrimaryKeySelective(retrievalInput);
            }

        } else if ("建设控制地带".equals(polygonType)) {
            String sql = "";
            String id = retrieval.getId();
//            if ("Polygon".equals(jsonType)) {
                sql = "UPDATE " + tableName + " SET extra_geometry = st_geomfromgeojson('" + geometry + "') WHERE id = '" + id + "'";
                dictMapper.updateObjectBatch(sql);
//            }

        } else {
            return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }

    /**
     * 基础数据管理导入
     *
     * @param object
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public ResultInfo jsonInputByEnd(String object) {
        String tableName = doValue(object, "name");
        String features = doValue(object, "features");
        switch (tableName) {
            case "吴文化地名":
                tableName = "wu_culture_place";
                break;
            case "传统产业":
                tableName = "traditional_industry";
                break;
            case "历史文化街区":
                tableName = "historical_street";
                break;
            case "历史地段":
                tableName = "historical_area";
                break;
            case "河道水系":
                tableName = "watercourse";
                break;
            case "文控保单位":
                tableName = "protection_unit";
                break;
            case "尚未核定为文物保护单位的不可移动文物":
                tableName = "ucr";
                break;
            case "历史建筑":
                tableName = "historical_building";
                break;
            case "苏州园林":
                tableName = "suzhou_gardens";
                break;
            case "近现代重要史迹及代表性建筑":
                tableName = "hsataoc";
                break;
            case "近现代重要史迹及代表性建筑(单体)":
                tableName = "hsataoc_individual";
                break;
            case "古城墙":
                tableName = "old_wall";
                break;
            case "传统民居":
                tableName = "traditional_houses";
                break;
            case "古树名木":
                tableName = "old_tree";
                break;
            case "工业遗产":
                tableName = "industrial_heritage";
                break;
            case "古构筑物":
                tableName = "old_structure";
                break;
            case "非物质文化遗产":
                tableName = "nch";
                break;
            case "整体格局与风貌":
                tableName = "layout_style";
                break;
            case "地下文物埋藏区":
                tableName = "uar";
                break;
            case "X":
                tableName = "others";
                break;
            default:
                break;
        }
        List<String> keys = dictMapper.selectColumnKeyResult(tableName);
        List<String> values = dictMapper.selectColumnValueResult(tableName);
        JSONArray array = JSONArray.parseArray(features);
        for (int i = 0; i < array.size(); i++) {
            String key1 = "";
            String values1 = "";
            String point = "";
            String Name = "";
            String code = "";
            String polygon1 = "";
            String polygon2 = "";
            String polygon3 = "";
            String singCode = "code_" + tableName;
            Map mapObject = (Map) JSON.parse(array.get(i).toString());
            String geometry = mapObject.get("geometry").toString();
            Map map = (Map) JSON.parse(geometry);
            String jsonType = map.get("type").toString();
            if (jsonType.equals("Polygon")) {
                String coordinates = map.get("coordinates").toString();
                JSONArray arr = JSONArray.parseArray(coordinates);
                for (int a = 0; a < arr.size(); a++) {
                    polygon1 = arr.get(a).toString();
                    JSONArray arr1 = JSONArray.parseArray(polygon1);
                    for (int b = 0; b < arr1.size(); b++) {
                        polygon2 = arr1.get(b).toString();
                        JSONArray arr2 = JSONArray.parseArray(polygon2);
                        for (int c = 0; c < arr2.size(); c++) {
                            polygon3 += arr2.get(c).toString() + " ";
                        }
                        if (b < (arr1.size() - 1)) {
                            polygon3 += ",";
                        }
                    }
                }
            }
            String properties = mapObject.get("properties").toString();
            Map map1 = (Map) JSON.parse(properties);
            if (map1.get("X") != null && map1.get("Y") != null) {
                String X = map1.get("X").toString();
                String Y = map1.get("Y").toString();
                point = X + " " + Y;
            }
            Set<String> set = map1.keySet();
            for (String key : set) {
                if (values.contains(key)) {
                    String val = String.valueOf(map1.get(key));
                    String n = keys.get(values.indexOf(key));
                    if (n.equals("name")) {
                        Name = String.valueOf(map1.get(key));
                    }
                    if (keys.get(values.indexOf(key)).equals(singCode)) {
                        code = String.valueOf(map1.get(key));
                    }
                    if (key1.equals("")) {
                        key1 += keys.get(values.indexOf(key));
                    } else {
                        key1 += "," + keys.get(values.indexOf(key));
                    }
                    if (values1.equals("")) {
                        values1 += "'" + val + "'";
                    } else {
                        values1 += ",'" + val + "'";
                    }
                }
            }
            Retrieval retrieval = retrievalMapper.selectByName(new Retrieval(Name, tableName));
            if (retrieval == null) {
                Date date = new Date();
                long longtime = date.getTime();
                java.sql.Date time = new java.sql.Date(longtime);
                String id = UUIDBuild.getUUID();
                String sql1 = "insert into object_manager(id,name,table_name,time ,status) values ('" + id + "','" + Name + "','" + tableName + "','" + time + "','已入库')";
                String sql = "";
                if (jsonType.equals("Polygon") && map1.get("X") != null && map1.get("Y") != null) {
                    sql = "insert into " + tableName + "(id," + key1 + ",geometry,point) values ('" + id + "'," + values1 + ",ST_GeomFromText('POLYGON((" + polygon3 + "))'),ST_GeomFromText('point(" + point + ")'))";
                } else if (jsonType.equals("Point") && map1.get("X") != null && map1.get("Y") != null) {
                    sql = "insert into " + tableName + "(id," + key1 + ",point) values ('" + id + "'," + values1 + ",ST_GeomFromText('point(" + point + ")'))";
                } else {
                    sql = "insert into " + tableName + "(id," + key1 + ") values ('" + id + "'," + values1 + ")";
                }
                dictMapper.insertObjectBatch(sql);
                retrievalMapper.insertObjectBatch(sql1);
                DcsInputCode dcsInputCode = DcsInputCode.builder().id(id).code(code).status("2").input_time(time).in_time(time).aduit_time(time).build();
                dcsInputCodeMapper.insert(dcsInputCode);
            } else {
                Date date = new Date();
                long longtime = date.getTime();
                java.sql.Date time = new java.sql.Date(longtime);
                String id = retrieval.getId();
                String sql1 = "insert into object_manager(id,name,table_name,time ,status) values ('" + id + "','" + Name + "','" + tableName + "','" + time + "','已入库')";
                String sql = "";
                if (jsonType.equals("Polygon") && map1.get("X") != null && map1.get("Y") != null) {
                    sql = "insert into " + tableName + "(id," + key1 + ",geometry,point) values ('" + id + "'," + values1 + ",ST_GeomFromText('POLYGON((" + polygon3 + "))'),ST_GeomFromText('point(" + point + ")'))";
                } else if (jsonType.equals("Point") && map1.get("X") != null && map1.get("Y") != null) {
                    sql = "insert into " + tableName + "(id," + key1 + ",point) values ('" + id + "'," + values1 + ",ST_GeomFromText('point(" + point + ")'))";
                } else {
                    sql = "insert into " + tableName + "(id," + key1 + ") values ('" + id + "'," + values1 + ")";
                }
                String deletesql1 = "delete from object_manager where name = '" + Name + "'";
                String deletesql = "delete from " + tableName + " where name = '" + Name + "'";
                dictMapper.deleteObjectBatch(deletesql);
                dictMapper.insertObjectBatch(sql);
                retrievalMapper.deleteObjectBatch(deletesql1);
                retrievalMapper.insertObjectBatch(sql1);
                DcsInputCode dcsInputCode = DcsInputCode.builder().id(id).code(code).status("2").input_time(time).aduit_time(time).in_time(time).build();
                dcsInputCodeMapper.updateStatus(dcsInputCode);
            }
        }
        List<DcsPerformance> list = dcsPerformanceMapper.selectCode();
        if (list.size() > 0) {
            for (DcsPerformance dcsPerformance : list) {
                String stayCount = String.valueOf(dcsPerformance.getCount());
                String taskCode = dcsPerformance.getTask_code();
                DcsTaskPerformance dcsTaskPerformance = DcsTaskPerformance.builder().stay_count(stayCount).task(taskCode).build();
                List<DcsCollectCode> listNumber = dcsCollectCodeMapper.selectCountByCode(taskCode);
                int a = listNumber.size();
                String starsNumber = "0";
                if (a > 0) {
                    double b = (double) (dcsPerformance.getCount() * 5) / a;
                    starsNumber = String.format("%.1f", b);
                }
                dcsTaskPerformance.setStars_number(starsNumber);
                dcsTaskPerformanceMapper.updateStayCount(dcsTaskPerformance);
            }
        }
        List<DcsTaskPerformance> listAll = dcsTaskPerformanceMapper.selectAll(null);
        if (listAll.size() > 0) {
            for (DcsTaskPerformance dcsTaskPerformance : listAll) {
                int total = Integer.valueOf(dcsTaskPerformance.getTotal_count());
                int stay = Integer.valueOf(dcsTaskPerformance.getStay_count());
                int complete = 0;
                if (total >= stay) {
                    complete = total - stay;
                }
                String complete_count = String.valueOf(complete);
                dcsTaskPerformance.setComplete_count(complete_count);
                dcsTaskPerformanceMapper.updateCompleteCount(dcsTaskPerformance);
            }
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }


    /**
     * Description 比较json文件和数据库字段
     * @Date 2019/7/31
     * @param jsonStr
     * @return com.acp.result.JsonData
     */
    @Override
    public JsonData compare(String jsonStr) {
        String tableName = doValue(jsonStr, "name");
        String features = doValue(jsonStr, "features");

        List result = new ArrayList();
        List<Retrieval> jsonList = new ArrayList<>();
        List<Retrieval> nameList = new ArrayList<>();

        List<String> dbList = dictMapper.selectColumnValueResult(nameTo(tableName));
        for (String db : dbList) {
            if (db.equals("主键")) {
                dbList.remove(db);
                break;
            }
        }
        dbList.forEach(db -> nameList.add(new Retrieval(db)));

        JSONArray array = JSONArray.parseArray(features);
        Map mapObject = (Map) JSON.parse(array.get(0).toString());
        String properties = mapObject.get("properties").toString();
        Map map1 = (Map) JSON.parse(properties);
        Set<String> set = map1.keySet();
        for (String key : set) {
            jsonList.add(new Retrieval(key));
        }

        result.add(nameList);
        result.add(jsonList);

        return JsonData.success(result, 906);
    }

    /**
     * @Description 表名转换
     * @Date 2019/7/31
     * @param tableName
     * @return java.lang.String
     */
    public String nameTo(String tableName){
        switch (tableName) {
            case "吴文化地名":
                tableName = "wu_culture_place";
                break;
            case "传统产业":
                tableName = "traditional_industry";
                break;
            case "历史文化街区":
                tableName = "historical_street";
                break;
            case "历史地段":
                tableName = "historical_area";
                break;
            case "河道水系":
                tableName = "watercourse";
                break;
            case "文控保单位":
                tableName = "protection_unit";
                break;
            case "文控保单位(单体)":
                tableName = "protection_unit_individual";
                break;
            case "尚未核定为文物保护单位的不可移动文物":
                tableName = "ucr";
                break;
            case "尚未核定为文物保护单位的不可移动文物(单体)":
                tableName = "ucr_individual";
                break;
            case "历史建筑":
                tableName = "historical_building";
                break;
            case "历史建筑(单体)":
                tableName = "historical_building_individual";
                break;
            case "苏州园林":
                tableName = "suzhou_gardens";
                break;
            case "苏州园林(单体)":
                tableName = "suzhou_gardens_individual";
                break;
            case "近现代重要史迹及代表性建筑":
                tableName = "hsataoc";
                break;
            case "近现代重要史迹及代表性建筑(单体)":
                tableName = "hsataoc_individual";
                break;
            case "古城墙":
                tableName = "old_wall";
                break;
            case "传统民居":
                tableName = "traditional_houses";
                break;
            case "传统民居(单体)":
                tableName = "traditional_houses_individual";
                break;
            case "古树名木":
                tableName = "old_tree";
                break;
            case "工业遗产":
                tableName = "industrial_heritage";
                break;
            case "工业遗产(单体)":
                tableName = "industrial_heritage_individual";
                break;
            case "古构筑物":
                tableName = "old_structure";
                break;
            case "非物质文化遗产":
                tableName = "nch";
                break;
            case "整体格局与风貌":
                tableName = "layout_style";
                break;
            case "地下文物埋藏区":
                tableName = "uar";
                break;
            case "X":
                tableName = "others";
                break;
            default:
                break;
        }
        return tableName;
    }
}
